package com.dhcc.core.modules.system.service.impl;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.dao.PanFileMapper;
import com.dhcc.core.modules.system.entity.PanAttachment;
import com.dhcc.core.modules.system.entity.PanFile;
import com.dhcc.core.modules.system.entity.PanFolderFile;
import com.dhcc.core.modules.system.service.IPanAttachmentService;
import com.dhcc.core.modules.system.service.IPanFileService;
import com.dhcc.core.modules.system.service.IPanFolderFileService;

/**
 * 表数据服务层接口
 * 
 * @ClassName: PanFileServiceImpl
 * @Description: TODO
 * @author mk
 * @date 2018-08-30 20:44:24
 */
@Service
@Transactional(readOnly = true)
public class PanFileServiceImpl extends BaseServiceImpl<PanFileMapper, PanFile> implements IPanFileService {
    @Autowired
    private IPanFolderFileService panFolderFileService;
    @Autowired
    private IPanAttachmentService panAttachmentService;

    /**
     * 获取附件通过文件ID
     * @param fileIds
     * @return
     */
    public List<PanAttachment> getAttachmentsByFileIds(@Param("fileIds")Long[] fileIds){
        return this.baseMapper.getAttachmentsByFileIds(fileIds);
    }
    /**
     * 获取文件夹中的文件
     * @Title: getUserAttachmentsOfFolder 
     * @Description: TODO
     * @param page
     * @param folderId
     * @param userId
     * @param fileName
     * @param startDate
     * @param endDate
     * @return
     * @return: List<PanAttachment>
     */
    @Override
    public List<PanAttachment> getUserAttachmentsOfFolder(Page<PanAttachment> page, Long folderId, Long userId,
            String fileName, String startDate, String endDate) {
        return this.baseMapper.getUserAttachmentsOfFolder(page, folderId, userId, fileName, startDate, endDate);
    }


    /**
     * 文件上传
     * @param file
     * @param source
     * @param folderId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public PanFile fileUpload(MultipartFile file, Integer source,Long folderId) {
        PanFile panFile = null;
        PanAttachment attachment = this.panAttachmentService.saveFile(file);
        panFile = new PanFile();
        panFile.setUserId(ShiroKit.getUser().getId());
        panFile.setAttachmentId(attachment.getId());
        panFile.setSource(source);
        panFile.insert();
        panFile.setFileName(attachment.getOriginalName());
        panFile.setAttachment(attachment);
        //如果folderId不等于“0”说明需要添加该文件到folderId的文件夹中
        if(folderId!=Const.PAN_DEFAULT_FOLDER_ID && folderId != null){
            PanFolderFile folderManagement = new PanFolderFile();
            folderManagement.setFileId(panFile.getId());
            folderManagement.setFolderId(folderId);
            folderManagement.insert();
        }
        return panFile;
    }

    /**
     * 转移个人文件夹
     * @param fileIds
     * @param folderId
     */
    @SuppressWarnings("unchecked")
    @Override
    @Transactional(readOnly=false)
    public void moveToFolder(Long[] fileIds, Long folderId) {
        for (Long fileId : fileIds){
            if(folderId==Const.PAN_DEFAULT_FOLDER_ID){
                this.panFolderFileService.delete(Condition.create().eq("file_id", fileId));
            }else {
                PanFolderFile ff = this.panFolderFileService.selectOne(Condition.create().eq("file_id", fileId));
                if(CommonUtil.isNotEmpty(ff)){
                    ff.setFolderId(folderId);
                    ff.updateById();
                }else {
                    PanFolderFile panFolderFile = new PanFolderFile();
                    panFolderFile.setFileId(fileId);
                    panFolderFile.setFolderId(folderId);
                    panFolderFile.insert();
                }
            }
        }
    }
    /**
     * 批量删除文件
     * @Title: deleteFile 
     * @Description: TODO
     * @param userId
     * @param fileIds
     * @return: void
     */
    @SuppressWarnings("unchecked")
    @Override
    @Transactional
    public void deleteFile(Long userId,Long[] fileIds) {
        for(Long fileId : fileIds){
            PanFile file = this.selectById(fileId);
            // 删除文件的关联
            panFolderFileService.delete(Condition.create().eq("file_id", fileId));
            // 删除文件
            this.deleteById(fileId);
            // 删除附件
            panAttachmentService.deleteAttachmentById(file.getAttachmentId());
        }
    }
    /**
     * 文件夹下是否存在文件
     * @Title: hasFileOfFolder 
     * @Description: TODO
     * @param folderId
     * @return
     * @return: Boolean
     */
    @Override
    public Boolean hasFileOfFolder(Long folderId) {
        Integer count = this.baseMapper.countFileOfFolder(folderId);
        return count>0;
    }


}